<?php

namespace moonlight\sweet;

use yii\base\InvalidConfigException;
use yii\base\Widget;
use yii\helpers\Html;

class SweetAlert extends Widget
{
    const CONFIRM_SUCCESS = 'success';
    /**
     * confirm框 warning类型
     */
    const CONFIRM_WARNING = 'warning';
    /**
     * confirm框 danger类型
     */
    const CONFIRM_DANGER = 'danger';
    /**
     * confirm框类型
     *
     * @var string
     */
    public $confirmType;
    /**
     * 是否显示取消按钮, true:展示, false:不展示
     *
     * @var boolean
     */
    public $showCancelButton = true;
    /**
     * 取消按钮默认文字
     *
     * @var string
     */
    public $cancelButtonText = '取消';
    /**
     * 取消按钮默认Class
     *
     * @var string
     */
    public $cancelButtonClass = 'btn-default';
    /**
     * 确定按钮默认文字
     *
     * @var string
     */
    public $confirmButtonText = '确定';
    /**
     * 确定按钮默认Class
     *
     * @var string
     */
    public $confirmButtonClass = 'btn-success';
    /**
     * 标题
     *
     * @var string
     */
    public $title;
    /**
     * 内容
     *
     * @var string
     */
    public $content = '';
    /**
     * confirm点击执行的JS
     *
     * @var string
     */
    public $confirmJs;
    /**
     * 按钮名称
     *
     * @var string
     */
    public $btnName;
    /**
     * 按钮配置
     *
     * @var array
     */
    public $btnOptions = [];
    /**
     * @var bool
     */
    public $closeOnConfirm = false;

    /**
     * @inheritdoc
     */
    public function init()
    {
        if (!isset($this->btnOptions['id'])) {
            throw new InvalidConfigException("a链接必须设置id");
        }
        if (is_null($this->btnName)) {
            throw new InvalidConfigException("a链接必须设置名称");
        }
        if ($this->confirmType && !in_array($this->confirmType, [static::CONFIRM_DANGER, static::CONFIRM_SUCCESS, static::CONFIRM_WARNING])) {
            throw new \yii\base\NotSupportedException('不支持的Confirm类型');
        }
        parent::init();
    }

    /**
     * @inheritdoc
     */
    public function run()
    {
        $this->registerAssetBundle();

        $this->generateJs();

        return $this->generateBtn();
    }

    /**
     * @inheritdoc
     */
    public function generateBtn()
    {
        return Html::a($this->btnName, 'javascript:;', $this->btnOptions);
    }

    /**
     * @inheritdoc
     */
    public function registerAssetBundle()
    {
        SweetAlertAsset::register($this->getView());
    }

    /**
     * @inheritdoc
     */
    public function generateJs()
    {
        $title = $this->title ? : $this->btnName . '?';
        $type = $this->confirmType ? "type: \"{$this->confirmType}\"," : null;
        $jsFunction = $this->confirmJs ?
            "function(){
                {$this->confirmJs}
            }" : null;
        $js = "
$(\"a#{$this->btnOptions['id']}\").on('click', function () {
    swal(
        {
            title: '{$title}',
            text: '{$this->content}',"
            . $type .
            "showCancelButton: {$this->showCancelButton},
            cancelButtonText: '{$this->cancelButtonText}',
            cancelButtonClass: '{$this->cancelButtonClass}',
            confirmButtonClass: '{$this->confirmButtonClass}',
            confirmButtonText: '{$this->confirmButtonText}',
            closeOnConfirm: false
        },
        " . $jsFunction . "
    );
})
";
        $this->getView()->registerJs($js);
    }
}